package com.ruoyi.quartz.task;
/*
 * @ClassName PointsTask
 * @Description //TODO
 * @Author Administrator
 * @Date 2024/4/3 15:20
 **/

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class PointsTask {

    @Autowired
    private IPointsRuleService pointsRuleService;
    @Autowired
    private IPointsRuleSystemService pointsRuleSystemService;
    @Autowired
    private IPointsManagementService pointsManagementService;


    /**
     * 修改积分规则状态
     */
    @Scheduled(cron = "0 0 2 1 1 ?")
    public void updatePointsRuleStatusTask(){
        //去年规则过期
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        String lastYear = DateUtils.getLastYear();
        pointsRule.setEffectiveTerm(lastYear);
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);
        if(!ObjectUtils.isEmpty(pointsRuleResult)){
            PointsRule pointsRule1Vo = new PointsRule();
            pointsRule1Vo.setId(pointsRuleResult.getId());
            pointsRule1Vo.setStatus("1");
            pointsRuleService.updatePointsRule(pointsRule1Vo);
        }
        //如果有今年的规则修改为正常状态
        String nowYear = DateUtils.getNowYear();
        pointsRule.setEffectiveTerm(nowYear);
        PointsRule pointsRuleVo = new PointsRule();
        pointsRuleVo.setEffectiveTerm(nowYear);
        PointsRule pointsRuleNowYear = pointsRuleService.selectPointsRuleByPointsRule(pointsRuleVo);
        pointsRuleNowYear.setStatus("0");
        pointsRuleService.updatePointsRule(pointsRuleNowYear);
    }


    /**
     * 执行积分系统规则（周）
     */
    @Scheduled(cron = "0 1 0 ? * 2")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void updateSystemPointsByWeek(){
        //查询出正常的规则
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);
        //查询出按周执行的系统规则
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setCyclicalPattern("0");
        pointsRuleSystem.setDelFlag("1");
        pointsRuleSystem.setPointsRuleId(pointsRuleResult.getId());
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        //上周一
        Date monday = DateUtils.getMonday();
        String mondayString = DateUtil.format(monday, "yyyy-MM-dd");
        //上周天
        DateTime dateTime = DateUtil.offsetDay(monday, 6);
        String sundayString = DateUtil.format(dateTime, "yyyy-MM-dd");
        pointsManagementService.insertPointsManagementSystem(pointsRuleSystems,mondayString,sundayString);
    }

    /**
     * 执行系统规则（月）
     */
    @Scheduled(cron = "0 1 0 1 * ?")
    public void updateSystemPointsByMonth(){
        //查询出正常的规则
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);
        //查询出按月执行的系统规则
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setCyclicalPattern("1");
        pointsRuleSystem.setDelFlag("1");
        pointsRuleSystem.setPointsRuleId(pointsRuleResult.getId());
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        //上月第一天
        Date firstDay = DateUtils.getLastMonthFirtDay();
        String firstDayString = DateUtil.format(firstDay, "yyyy-MM-dd");
        //上月最后一天
        Date lastDay = DateUtils.getLastMonthLastDay();
        String lastDayString = DateUtil.format(lastDay, "yyyy-MM-dd");
        pointsManagementService.insertPointsManagementSystem(pointsRuleSystems,firstDayString,lastDayString);
    }

    /**
     * 执行系统规则（季）每个季度的第一个月的一号的00:01:00 执行一次
     */
    @Scheduled(cron = "0 1 0 1 1,4,7,10 ?")
    public void updateSystemPointsByQuarter(){
        //查询出正常的规则
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);

        //查询出按季度执行的系统规则
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setCyclicalPattern("2");
        pointsRuleSystem.setDelFlag("1");
        pointsRuleSystem.setPointsRuleId(pointsRuleResult.getId());
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        //上个季度第一天
        Date firstDay = DateUtils.getLastQuarterFirstDay();
        String firstDayString = DateUtil.format(firstDay, "yyyy-MM-dd");
        //上个季度最后一天
        Date lastDay = DateUtils.getLastQuarterLastDay();
        String lastDayString = DateUtil.format(lastDay, "yyyy-MM-dd");
        pointsManagementService.insertPointsManagementSystem(pointsRuleSystems,firstDayString,lastDayString);
    }

    /**
     * 执行系统规则（半年）
     */
    @Scheduled(cron = "0 1 0 1 7 ?")
    public void updateSystemPointsByHalfYear1(){
        //查询出正常的规则
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);

        //查询出按半年执行的系统规则
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setCyclicalPattern("3");
        pointsRuleSystem.setDelFlag("1");
        pointsRuleSystem.setPointsRuleId(pointsRuleResult.getId());
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        //1月1日
        String yearFirstDay = DateUtils.getYearFirstDay();
        //6月30日
        String year = yearFirstDay.substring(0, 4);
        String lastDayString = year+"-06-30";
        pointsManagementService.insertPointsManagementSystem(pointsRuleSystems,yearFirstDay,lastDayString);
    }

    /**
     * 执行系统规则（半年）
     */
    @Scheduled(cron = "0 1 0 1 1 ?")
    public void updateSystemPointsByHalfYear2(){
        //查询出正常的规则
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);

        //查询出按半年执行的系统规则
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setCyclicalPattern("3");
        pointsRuleSystem.setDelFlag("1");
        pointsRuleSystem.setPointsRuleId(pointsRuleResult.getId());
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        //去年7月1号
        String lastYear = DateUtils.getLastYear();
        String firstDayString = lastYear+"-07-01";
        //去年12月31号
        String lastDayString = lastYear+"-12-31";
        pointsManagementService.insertPointsManagementSystem(pointsRuleSystems,firstDayString,lastDayString);
    }

    /**
     * 执行系统规则（年）
     */
    @Scheduled(cron = "0 1 0 1 1 ?")
    public void updateSystemPointsByYear(){
        //查询出正常的规则
        PointsRule pointsRule = new PointsRule();
        pointsRule.setStatus("0");
        PointsRule pointsRuleResult = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);

        //查询出按年执行的系统规则
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setCyclicalPattern("4");
        pointsRuleSystem.setDelFlag("1");
        pointsRuleSystem.setPointsRuleId(pointsRuleResult.getId());
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        //去年1月1号
        String lastYear = DateUtils.getLastYear();
        String firstDayString = lastYear+"-01-01";
        //去年12月31号
        String lastDayString = lastYear+"-12-31";
        pointsManagementService.insertPointsManagementSystem(pointsRuleSystems,firstDayString,lastDayString);
    }
}
